/**
 *  Google Picasa Web Album
 *  @version 1.0.1 build 2012.6.26
 *  @author  Mr.Asong
 *  @email   i@mrasong.com
 *  @link    http://mrasong.com
 */
jQuery.extend({
	pwa: {
		appinfo: {
			title: 'Google Picasa Web Album',
			version: '1.0.1',
			build: '2012-06-26'
		},
		title: 'My Album',
		subtitle: 'sub title',
		show_id: 'pwa_album',
		user_id: 'asong.mr', //Google帐号，如果是Gmail可以省去后缀如 example@gmail.com，可以直接写example
		pic_size : { small:160, middle:800, big:1600 } ,  //图片像素, 小->大  94, 110, 128, 200, 220, 288, 320, 400, 512, 576, 640, 720, 800, 912, 1024, 1152, 1280, 1440, 1600
		page_size: 20, //每页显示图片数量

		getHash: function(){//获取url hash
			var default_hash = "#album_list_1_show";
			hash = (!window.location.hash) ? default_hash : window.location.hash; 
			args = hash.split("_");
			if( typeof(args[1]) == "undefined" ){ args[1]="list"; }
			if( typeof(args[2]) == "undefined" || args[2]=="" ||  (typeof(args[2])=="string" && parseInt(args[2])==0) ){
				args[2]=1; 
			}
			return args;
		},
		getHashStr: function(id, page){ /* 生成url hash */
			if( id=="list" ){ return "#album_list_"+page+"_show"; }
			return "#album_"+id+"_"+page+"_show";
		},
		getUrl: function(id, page){ /* 生成json url */
			if( id == "list" ){ //首页
				id = ""; offset = ""; max_results = "";
			}else{ //图片列表页
				id = "/albumid/" + id;
				offset = "&start-index=" + ( $.pwa.page_size*(page-1)+1 ) ;
				max_results = "&max-results=" + $.pwa.page_size;
			}
			tmp_url = 'https://picasaweb.google.com/data/feed/api/user/' + $.pwa.user_id + id;
			tmp_url += '?alt=json' + offset + max_results + '&hl=zh_CN&callback=?';
			return tmp_url;
		},
		getFormatSize: function(size){/* 格式化图片大小 */
			size = parseInt(size);
			if( Math.round(size*100/(1024*1024))/100 > 1.00 ){
				return Math.round(size*100/(1024*1024))/100 + " MB";
			}
			if( Math.round(size*100/1024)/100 > 1.00 ){
				return Math.round(size*100/1024)/100 + " KB";
			}
			return  size + " B";
		},
		getFormatDate: function(time){ /* 格式化时间 */
			var d = new Date(parseInt(time));
			return d.getFullYear()+ "-" + (d.getMonth()+1) + "-" + d.getDate();
		},
		goBack: function(id){ /* 返回主页 */
			$('#pwa_album_list').fadeIn();
			$('#pwa_album_'+id).fadeOut();
		},
		
		goForward: function(id){ /* 显示列表 */
			$('#pwa_album_list').fadeOut();
			$('#pwa_album_'+id).fadeIn();
		},
		getImgHtml: function(img, title){ /* 生成图片html */
			return '<img src="'+ img +'?imgmax='+$.pwa.pic_size.small+'" alt="'+ img +'?imgmax='+$.pwa.pic_size.big+'" title="'+ title +'" onclick="$.pwa.mImgClick($(this));" />';
		},
		getAlbumHtml: function(item){ /* 获取相册列表 */
			tmp_html = '<div class="pwa_pics">';
			//tmp_html += '<p><img src="'+ item.media$group.media$thumbnail[0].url +'" alt="'+ item.media$group.media$content[0].url+'?imgmax='+ $.pwa.pic_size.big +'"  /></p>';
			//tmp_html += '<p>'+ $.pwa.getImgHtml(item.media$group.media$content[0].url, item.title.$t)+'</p>';
			tmp_html += '<p class="img">'+ $.pwa.getImgHtml(item.media$group.media$thumbnail[0].url, item.title.$t)+'</p>';
			tmp_html += '<p><a href="'+ $.pwa.getHashStr(item.gphoto$id.$t, 1) +'"  onclick="$.pwa.showPicList(\''+ item.gphoto$id.$t +'\','+ 1 +')">'+ item.title.$t +'</a> ';
			tmp_html += '<span>('+ item.gphoto$numphotos.$t +')</span> <span>'+ $.pwa.getFormatDate(item.gphoto$timestamp.$t) +'</span></p>';
			tmp_html += '<p>简介：'+item.summary.$t+'</p>';
			tmp_html += '</div>';
			return tmp_html;
		},
		getPicListHtml: function(item){ /* 获取照片列表 */
			tmp_html = '<div class="pwa_pics">';
			//tmp_html += '<p><img src="'+ item.content.src +'?imgmax='+ $.pwa.pic_size.small +'" alt="'+ item.content.src +'?imgmax='+ $.pwa.pic_size.big +'" title="'+item.summary.$t + '" onclick="$.pwa.eventImgsClick($(this));" /></p>';
			tmp_html += '<p class="img">'+ $.pwa.getImgHtml(item.content.src, item.summary.$t) +'</p>';
			tmp_html += '<p>'+ item.title.$t +'</p>';
			tmp_html += '<p>'+ item.summary.$t +'</p>';
			tmp_html += '<p>大小：'+ $.pwa.getFormatSize(item.gphoto$size.$t) +'  '+item.gphoto$width.$t+'*'+item.gphoto$height.$t+'</p>';
			tmp_html += '</div>';
			return tmp_html;
		},
		getHeaderHtml: function(){ /* 显示头部 */
			tmp_html = '<div id="pwa_header">';
			tmp_html += '<h1>『'+ $.pwa.title +'』</h1>';
			tmp_html += '<p>'+ $.pwa.subtitle +'</p><hr/>';
			tmp_html += '</div>';
			return tmp_html;
		},
		getFooterHtml: function(){ /* 显示底部 */
			tmp_html = '<div id="pwa_footer">';
			tmp_html += '<p><span><a href="http://mrasong.com/">&copy Mr.Asong</a></span></p>';
			tmp_html += '</div>';
			return tmp_html;
		},
		getSummaryHtml: function(item){ /* 显示顶部信息 */
			tmp_html = '<div class="pwa_summary"><p>';
			tmp_html += '<span>『'+ item.title.$t +'』</span>';
			tmp_html += '<span>'+ item.subtitle.$t +'</span>';
			tmp_html += '<span><a href="'+$.pwa.getHashStr('list', 1)+'" onclick="$.pwa.goBack(\''+ item.gphoto$id.$t +'\');">主页</a></span>';
			tmp_html += '</p></div>';
			return tmp_html;
		},
		getNavHtml: function(item, page){ /* 显示首页分页按钮 */
			var total = item.gphoto$numphotos.$t; //照片总数
			var album_id = item.gphoto$id.$t; //相册id
			var pages = Math.ceil(total/$.pwa.page_size); //总页数
			var tmp_html = "";

			if( pages<=10 && pages>1 ){ //显示全部
				for(var i=1; i<=pages; i++){
					tmp_html += '<a href="'+ $.pwa.getHashStr(album_id, i) +'" onclick="$.pwa.showPicList(\''+ album_id +'\', '+ i +')"';
					if( page==i ){ tmp_html += ' class="pwa_nav_current" ';}
					tmp_html += '>'+ i +'</a> ';
				}
			}else if( pages>10 ){ //大于10页时，只显示首页，第一页，下一页，末页
				if( page>1 ){
					tmp_html += '<a href="'+ $.pwa.getHashStr(album_id, 1) +'" onclick="$.pwa.showPicList(\''+ album_id +'\', '+ 1 +')">First</a> ';
					tmp_html += '<a href="'+ $.pwa.getHashStr(album_id, page-1) +'" onclick="$.pwa.showPicList(\''+ album_id +'\', '+ (page-1) +')">&laquo; Previous</a> ';
				}
				if( page<pages ){
					tmp_html += '<a href="'+ $.pwa.getHashStr(album_id, page+1) +'" onclick="$.pwa.showPicList(\''+ album_id +'\', '+ (page+1) +')">Next &raquo;</a> ';
					tmp_html += '<a href="'+ $.pwa.getHashStr(album_id, pages) +'" onclick="$.pwa.showPicList(\''+ album_id +'\', '+ pages +')">Last</a> ';
				}
				tmp_html += '<span>共['+ pages +']页，第['+ page +']页</span>';
			}
			if( pages>1 ){
				return '<div class="pwa_nav"><p>' + tmp_html + '</p></div>';
			}
			return tmp_html;
		},
		showPicList: function(id, page){ /* 图片列表 */
			$.getJSON($.pwa.getUrl(id, page), function(d){
				if( typeof(d.feed.entry)=='undefined' ){
					$.pwa.goBack(id); return false;
				}
				$('#pwa_album_'+id).html($.pwa.getSummaryHtml(d.feed));//显示概要
				$.each(d.feed.entry, function(i, item){
					$('#pwa_album_'+id).append( $.pwa.getPicListHtml(item) );
				});
				$('#pwa_album_'+id).append('<div class="pwa_clear"></div>');
				$('#pwa_album_'+id).append($.pwa.getNavHtml(d.feed, page));//显示分页
				$.pwa.goForward(id); //隐藏目录
			});
		},
		showAlbum: function(){ /* 显示相册列表 */
			$.getJSON($.pwa.getUrl('list', 1), function(d){
				$.each(d.feed.entry, function(i, item){
					$('#pwa_album_list').append( $.pwa.getAlbumHtml(item) );
					$('#pwa_content').append('<div class="pwa_album_list" id="pwa_album_'+ item.gphoto$id.$t +'"></div>');
				});
				$('#pwa_album_list').append('<div class="pwa_clear"></div>');
			});
		},
		mImgClick: function(obj){
			var tmp = obj.attr("src");
			obj.attr("src", obj.attr("alt") );
			obj.attr("alt", tmp );
		},
		mInit: function(){ //初始化加载
			$('#'+$.pwa.show_id).html( $.pwa.getHeaderHtml() 
				+'<div id="pwa_content"><div class="pwa_album_list" id="pwa_album_list"></div></div>' 
				+  $.pwa.getFooterHtml() );
			var hash = $.pwa.getHash();
			$.pwa.showAlbum();
			if( hash && hash[1]!="list" ){
				$.pwa.showPicList(hash[1], hash[2]);
			}
		}
	}	
});
